package com.codeplatform.mapper;

import com.codeplatform.entity.UserSession;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户会话数据访问接口
 */
@Mapper
public interface UserSessionMapper extends BaseMapper<UserSession> {
    
    /**
     * 根据会话令牌查找会话
     */
    @Select("SELECT * FROM user_sessions WHERE session_token = #{sessionToken}")
    UserSession findBySessionToken(@Param("sessionToken") String sessionToken);
    
    /**
     * 根据刷新令牌查找会话
     */
    @Select("SELECT * FROM user_sessions WHERE refresh_token = #{refreshToken}")
    UserSession findByRefreshToken(@Param("refreshToken") String refreshToken);
    
    /**
     * 根据用户ID查找所有会话
     */
    @Select("SELECT * FROM user_sessions WHERE user_id = #{userId}")
    List<UserSession> findByUserId(@Param("userId") Long userId);
    
    /**
     * 根据用户ID查找活跃会话
     */
    @Select("SELECT * FROM user_sessions WHERE user_id = #{userId} AND is_active = 1 AND expires_at > NOW()")
    List<UserSession> findActiveSessionsByUserId(@Param("userId") Long userId);
    
    /**
     * 根据用户ID和设备信息查找会话
     */
    @Select("SELECT * FROM user_sessions WHERE user_id = #{userId} AND device_info = #{deviceInfo}")
    UserSession findByUserIdAndDeviceInfo(@Param("userId") Long userId, @Param("deviceInfo") String deviceInfo);
    
    /**
     * 查找过期的会话
     */
    @Select("SELECT * FROM user_sessions WHERE expires_at < NOW() OR refresh_expires_at < NOW()")
    List<UserSession> findExpiredSessions();
    
    /**
     * 查找指定时间之前的非活跃会话
     */
    @Select("SELECT * FROM user_sessions WHERE last_active_at < #{threshold}")
    List<UserSession> findInactiveSessions(@Param("threshold") LocalDateTime threshold);
    
    /**
     * 删除用户的所有会话
     */
    @Delete("DELETE FROM user_sessions WHERE user_id = #{userId}")
    void deleteByUserId(@Param("userId") Long userId);
    
    /**
     * 删除过期的会话
     */
    @Delete("DELETE FROM user_sessions WHERE expires_at < NOW() OR refresh_expires_at < NOW()")
    void deleteExpiredSessions();
    
    /**
     * 删除指定时间之前的非活跃会话
     */
    @Delete("DELETE FROM user_sessions WHERE last_active_at < #{threshold}")
    void deleteInactiveSessions(@Param("threshold") LocalDateTime threshold);
    
    /**
     * 使会话失效
     */
    @Update("UPDATE user_sessions SET is_active = 0 WHERE session_token = #{sessionToken}")
    void invalidateSession(@Param("sessionToken") String sessionToken);
    
    /**
     * 使用户的所有会话失效
     */
    @Update("UPDATE user_sessions SET is_active = 0 WHERE user_id = #{userId}")
    void invalidateUserSessions(@Param("userId") Long userId);
    
    /**
     * 更新最后活跃时间
     */
    @Update("UPDATE user_sessions SET last_active_at = NOW(), updated_at = NOW() WHERE session_token = #{sessionToken}")
    void updateLastActiveTime(@Param("sessionToken") String sessionToken);
    
    /**
     * 统计用户的活跃会话数
     */
    @Select("SELECT COUNT(*) FROM user_sessions WHERE user_id = #{userId} AND is_active = 1 AND expires_at > NOW()")
    long countActiveSessionsByUserId(@Param("userId") Long userId);
    
    /**
     * 统计总的活跃会话数
     */
    @Select("SELECT COUNT(*) FROM user_sessions WHERE is_active = 1 AND expires_at > NOW()")
    long countActiveSessions();
    
    /**
     * 根据IP地址查找会话
     */
    @Select("SELECT * FROM user_sessions WHERE ip_address = #{ipAddress}")
    List<UserSession> findByIpAddress(@Param("ipAddress") String ipAddress);
    
    /**
     * 根据会话令牌删除会话
     */
    @Delete("DELETE FROM user_sessions WHERE session_token = #{sessionToken}")
    void deleteBySessionToken(@Param("sessionToken") String sessionToken);
    
    /**
     * 查找最近的会话
     */
    @Select("SELECT * FROM user_sessions WHERE created_at >= #{since} ORDER BY created_at DESC")
    List<UserSession> findRecentSessions(@Param("since") LocalDateTime since);
}